package com.sun.module_network.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities


/**
 *@author:sunc
 *@time :2025/7/18 14:14
 *@desc: 网络检查工具类
 */
object NetWorkUtils {
    /**
     * api 21+ 判断网络是否可用
     */
    fun isNetworkAvailable(context: Context): Boolean {
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (connectivityManager != null) {
            val capabilities =
                connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
            if (capabilities != null) {
                if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
                    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
                    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) ||
                    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
                ) {
                    // This device has a network of the specified type
                    return true
                }
            }
        }
        return false
    }


}